//
//  LocationManager.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/8/4.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import MapKit
import CoreLocation

typealias FinishLocationBlock = (_ location: CLLocation, _ placemark:CLPlacemark,_ LocationManager: CLLocationManager)->Void

class LocationManager: NSObject {
    
    let  locationManager = CLLocationManager()

    var finishBlock : FinishLocationBlock!
    
    var isAllLocation = false
    
    static let  sharInstance = LocationManager()
    
    
     func startLocation() {
        
        if CLLocationManager.locationServicesEnabled() {
            
            LocationManager.sharInstance.locationManager.delegate = self
            
            if #available(iOS 8.0, *) {
                
                self.locationManager.distanceFilter = kCLDistanceFilterNone
             
                self.locationManager.requestWhenInUseAuthorization()
                self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
            }
            
            self.locationManager.startUpdatingLocation()
            
        }
    }
    
    
    
     func getLoction(_ isAlways:Bool,LoctionBlock: @escaping FinishLocationBlock){
        
        
       isAllLocation = isAlways
        
       startLocation()
        
       finishBlock = LoctionBlock
        
    }


    

}



extension LocationManager : CLLocationManagerDelegate,MKMapViewDelegate{

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        
        if status == CLAuthorizationStatus.notDetermined || status == CLAuthorizationStatus.restricted || status == CLAuthorizationStatus.denied {
            
            if #available(iOS 8.0, *) {
                
                if self.isAllLocation == true {
                    
                    if #available(iOS 9.0, *) {
                        manager.allowsBackgroundLocationUpdates = true
                    }
                    
                    if self.locationManager.responds(to:#selector(CLLocationManager.requestWhenInUseAuthorization)) {
                        self.locationManager.requestWhenInUseAuthorization()
                    }
                    
                }else{
                    
                    if self.locationManager.responds(to: #selector(CLLocationManager.requestAlwaysAuthorization)) {
                        self.locationManager.requestAlwaysAuthorization()
                    }
                }
                
            }
            
        }
        
        manager.startUpdatingLocation()
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        
        let newLocation:CLLocation = locations.first!

        
        let geocoder = CLGeocoder()
        geocoder.reverseGeocodeLocation(newLocation) { (placemarkBlock, error) in
            
            if (error != nil){
                manager.stopUpdatingLocation()
            }else{
                let  placemark = placemarkBlock?.first
                
                self.finishBlock(newLocation,placemark!,manager)
                
            }
            
        }
        
        if !self.isAllLocation {
            self.locationManager.stopUpdatingLocation()
        }
    }
    
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        self.locationManager.stopUpdatingLocation()
    }
    
    func locationManager(_ manager: CLLocationManager, didFinishDeferredUpdatesWithError error: Error?) {
        self.locationManager.stopUpdatingLocation()
    }
    









}
